package com.huluobo.report.view

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.google.gson.Gson
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_base.UIState
import com.huluobo.report.ReportIntent
import com.huluobo.report.ReportViewModel
import com.huluobo.report.entity.ReportEntityItem
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/1/16.
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ReportPage(navHostController: NavHostController, vm: ReportViewModel = hiltViewModel()) {
    val list = remember {
        SnapshotStateList<ReportEntityItem>()
    }

    var isShow by remember {
        mutableStateOf(false)
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.onError -> {
                        Log.e("TagA", "error:${it.msg}")
                    }

                    UIState.onLoading -> {}
                    is UIState.onSuccess<*> -> {
                        list.addAll(it.data as Collection<ReportEntityItem>)
                    }
                }
            }
        }

        vm.sendIntent(ReportIntent.getReport)
    })

    Scaffold(
        floatingActionButton = {
            FloatingActionButton(onClick = {
                isShow = true
            }) {
                Icon(imageVector = Icons.Default.Add, contentDescription = "")
            }
        }
    ) {
        Column(
            Modifier
                .padding(it)
                .padding(10.dp)
        ) {
            BaseTitle(title = "工作日志", navHostController = navHostController)
            Spacer(modifier = Modifier.height(10.dp))
            LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp)) {
                items(list) {
                    ReportItem(entity = it)
                }
            }
        }
    }

    if (isShow) {
        val dialogList = listOf(
            ReportDialogEntity(1, "1", "日报"),
            ReportDialogEntity(2, "7", "周报"),
            ReportDialogEntity(3, "30", "月报"),
            ReportDialogEntity(4, "季度", "季度报表"),
            ReportDialogEntity(5, "业绩", "业绩报表"),
            ReportDialogEntity(6, "营业", "营业财报"),
        )

        AlertDialog(
            modifier = Modifier
                .fillMaxWidth()
                .background(Color.White, RoundedCornerShape(20.dp))
                .padding(20.dp),
            onDismissRequest = { isShow = false }) {
            Column {
                Text(text = "新建汇报")
                Spacer(modifier = Modifier.height(10.dp))
                LazyVerticalGrid(verticalArrangement = Arrangement.spacedBy(10.dp),
                    horizontalArrangement = Arrangement.Center,
                    columns = GridCells.Fixed(3),
                    content = {
                        items(dialogList) {
                            DialogItem(entity = it, navHostController = navHostController)
                        }
                    })
            }
        }
    }
}

data class ReportDialogEntity(val type: Int, val title: String, val name: String)

@Composable
fun DialogItem(entity: ReportDialogEntity, navHostController: NavHostController) {
    Column(Modifier.clickable {
        val json = Gson().toJson(entity)
        //传递json
        navHostController.navigate(RoutePath.COMMIT_REPORT + "/$json")
    }, horizontalAlignment = Alignment.CenterHorizontally) {
        Column(
            Modifier
                .size(50.dp)
                .background(Color.LightGray, CircleShape)
                .padding(5.dp)
                .border(1.dp, Color.Blue, RoundedCornerShape(5.dp))
                .background(Color.White, RoundedCornerShape(5.dp)),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(text = entity.title)
        }
        Text(text = entity.name)
    }
}

@Composable
fun ReportItem(entity: ReportEntityItem) {
    Column(verticalArrangement = Arrangement.spacedBy(5.dp)) {
        Row {
            AsyncImage(
                model = "https://img2.baidu.com/it/u=553712968,3437602612&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1729875600&t=e4486de729ce7a3ac4e9762390725f73",
                contentDescription = "",
                modifier = Modifier
                    .size(50.dp)
                    .clip(CircleShape),
                contentScale = ContentScale.Crop
            )
            Spacer(modifier = Modifier.width(10.dp))
            Column(Modifier.height(50.dp), verticalArrangement = Arrangement.SpaceBetween) {
                Text(text = "日报")
                Text(text = entity.createTime)
            }
        }
        Text(text = "工作内容:${entity.content}")
        Text(text = "日报状态:${entity.status}")
        Text(text = "发布人员:${entity.staffName}")
        Text(text = "部门:${entity.deptName}")
    }
}